package ga.model;

import ga.crossover.Crossover;
import ga.selection.Selection;

import java.util.ArrayList;

public class Algorythm implements Runnable {
	Selection selection;
	Crossover crossover;
	ArrayList<Chromosome> population;
	int generations;
	ArrayList<ArrayList<Chromosome>> statistics;

	public Algorythm(Selection selection, Crossover crossover,
			ArrayList<Chromosome> population, int generations) {
		super();
		this.selection = selection;
		this.crossover = crossover;
		this.population = population;
		this.generations = generations;
		statistics= new ArrayList<ArrayList<Chromosome>>();
		statistics.add(population);
	}

	@Override
	public void run() {
		Chromosome[] parents;
		Chromosome[] childs;
		for (int i = 0; i < generations; i++) {
			ArrayList<Chromosome> newPopulation = new ArrayList<Chromosome>();
			while (newPopulation.size() < population.size()) {
				parents = selection.selectPatents(population);
				childs = crossover.crossover(parents);
				if (childs.length == 2) {
					newPopulation.add(childs[0]);
					newPopulation.add(childs[1]);

				} else {
					System.out.println("crossover didnt made well");
				}
			}
			population=newPopulation;
			statistics.add(population);
		}
	}

	public ArrayList<ArrayList<Chromosome>> getStatistics() {
		return statistics;
	}

}
